package com.sunlg.smarthome.ui.gateway;

import android.content.DialogInterface;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.contract.GatewayContract;
import com.sunlg.smarthome.contract.presenter.GatewayPresenter;
import com.sunlg.smarthome.data.entity.Gateway;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GatewayActivity extends BaseActivity
        implements GatewayContract.View, View.OnClickListener,
        BindGatewayDialog.OnEditTextInputListener{

    @BindView(R.id.iv_status)
    ImageView ivStatus;

    @BindView(R.id.rl_gateway_info)
    RelativeLayout rlGatewayInfo;

    @BindView(R.id.fab_bind)
    FloatingActionButton fabBind;
    @BindView(R.id.ll_no)
    LinearLayout llNo;
    @BindView(R.id.ll_desc)
    LinearLayout llDesc;

    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.tv_ip)
    TextView tvIP;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_none_gateway)
    TextView tvNoneGateway;

    GatewayContract.Presenter presenter;
    BindGatewayDialog bindGatewayDialog;
    UpdateNoDialog updateNoDialog;
    UpdateDescDialog updateDescDialog;
    private List<Gateway> mGatewayList;
    private Gateway currentGateway;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_gateway;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("网关");
        actionBar.setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        bindGatewayDialog = new BindGatewayDialog(this);
        updateNoDialog = new UpdateNoDialog(this);
        updateDescDialog = new UpdateDescDialog(this);

        mGatewayList = new ArrayList<>();

        presenter = new GatewayPresenter();
        presenter.attachView(this);

        presenter.init();
    }

    @Override
    protected void initView() {
        fabBind.setOnClickListener(this);
        llNo.setOnClickListener(this);
        llDesc.setOnClickListener(this);

        bindGatewayDialog.setOnEditTextInputListener(this);

        updateNoDialog.setOnEditTextInputListener(new UpdateNoDialog.OnEditTextInputListener() {
            @Override
            public void onEditTextInput(Integer no) {
                Gateway gateway = new Gateway();
                if(currentGateway != null){
                    gateway.setId(currentGateway.getId());
                }
                if(!TextUtils.isEmpty(String.valueOf(no))){
                    gateway.setNo(no);
                }
                presenter.updateGateway(gateway);

            }
        });
        updateDescDialog.setOnEditTextInputListener(new UpdateDescDialog.OnEditTextInputListener() {
            @Override
            public void onEditTextInput(String desc) {
                Gateway gateway = new Gateway();
                if(currentGateway != null){
                    gateway.setId(currentGateway.getId());
                }
                if(!TextUtils.isEmpty(desc)){
                    gateway.setDescription(desc);
                }
                presenter.updateGateway(gateway);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_gateway, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_refresh:
                presenter.loadGateway();
                break;
            case R.id.action_add:
                bindGatewayDialog.show();
                break;
            case R.id.action_delete:
                new AlertDialog.Builder(this)
                        .setTitle("警告")
                        .setMessage("如果删除网关，则与网关关联的设备及其数据也会被删除!\n确认是否删除?")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                presenter.deleteGateway();
                            }
                        })
                        .show();

                break;
            case R.id.action_switch:
                presenter.switchGateway();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void updateGatewaySuccess() {
        presenter.init();
    }

    @Override
    public void showSwitchDialog(List<Gateway> gateways) {
        this.mGatewayList.clear();
        this.mGatewayList.addAll(gateways);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("切换网关");
        //    指定下拉列表的显示数据
        List<String> itemList = new ArrayList<>();
        for (Gateway g : gateways){
            itemList.add(g.getNo()+" "+g.getDescription());
        }
        String[] items = new String[itemList.size()];
        //    设置一个下拉的列表选择项
        builder.setItems(itemList.toArray(items), new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                presenter.loadGateway(mGatewayList.get(which).getId());
            }
        });
        builder.show();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_no:
                updateNoDialog.show("修改序号", "序号");
                break;
            case R.id.ll_desc:
                updateDescDialog.show("修改描述", "描述");
                break;
            case R.id.fab_bind:
                bindGatewayDialog.show();
                break;
        }
    }

    @Override
    public void loadAllGatewaySuccess(List<Gateway> gateways) {
        this.mGatewayList.clear();
        this.mGatewayList.addAll(gateways);
    }

    @Override
    public void hideBindDialog() {
        if(bindGatewayDialog.isShowing()) {
            bindGatewayDialog.hide();
        }
    }

    /**
     * 绑定网关对话框中编辑框的监听器
     * @param no
     * @param desc
     */
    @Override
    public void onEditTextInput(Integer no, String desc) {

        Gateway gateway = new Gateway();
        gateway.setNo(no);
        gateway.setDescription(desc);

        presenter.addGateway(gateway);
    }

    @Override
    public void showOfflineImg() {
        if(ivStatus.getVisibility() != View.VISIBLE){
            ivStatus.setVisibility(View.VISIBLE);
        }
        ivStatus.setBackgroundResource(R.mipmap.img_offline);
    }

    @Override
    public void showNoneGatewayImg() {
        if(ivStatus.getVisibility() != View.VISIBLE){
            ivStatus.setVisibility(View.VISIBLE);
        }
        ivStatus.setBackgroundResource(R.mipmap.img_none_gateway);
    }

    @Override
    public void hideGatewayStatusImg() {
        if(ivStatus.getVisibility() == View.VISIBLE){
            ivStatus.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showNoneGatewayText() {
        if(tvNoneGateway.getVisibility() != View.VISIBLE){
            tvNoneGateway.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideNoneGatewayText() {
        if(tvNoneGateway.getVisibility() == View.VISIBLE){
            tvNoneGateway.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showGatewayInfo(Gateway gateway) {

        currentGateway = gateway;

        if(rlGatewayInfo.getVisibility() != View.VISIBLE){
            rlGatewayInfo.setVisibility(View.VISIBLE);
        }
        Integer no = gateway.getNo();
        String ip = gateway.getIp();
        boolean status = gateway.getStatus();
        String desc = gateway.getDescription();
        if(no != null){
            tvNo.setText(String.valueOf(no));
        }
        if(!TextUtils.isEmpty(ip)){
            tvIP.setText(ip);
        }else {
            tvIP.setText("");
        }
        if(!TextUtils.isEmpty(desc)){
            tvDesc.setText(desc);
        }else {
            tvDesc.setText("");
        }
        if(status == true){
            tvStatus.setText("在线");
        }else {
            tvStatus.setText("离线");
        }
    }

    @Override
    public void hideGatewayInfo() {
        if(rlGatewayInfo.getVisibility() == View.VISIBLE){
            rlGatewayInfo.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showFab() {
        fabBind.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideFab() {
        fabBind.setVisibility(View.GONE);
    }

    @Override
    public void showBindProgress() {
        showLoadBar("正在绑定...");
    }

    @Override
    public void hideBindProgress() {
        hideLoadBar();
    }
    @Override
    public void showError(String msg) {
        toast(msg);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.dettachView();
    }
}
